package com.tudouji.project.wlinfo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.flow.service.pojo.ResultParam;
import com.flow.service.pojo.result.FlowResult;
import com.flow.service.process.service.impl.FlowProcessProxy;
import com.tudouji.common.enums.WlinfoPayStatusEnum;
import com.tudouji.common.enums.WlinfoStatusEnum;
import com.tudouji.common.enums.WlinfoStatusEnums;
import com.tudouji.common.enums.WlinfoitemStatusEnum;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.http.HttpBusinessUtils;
import com.tudouji.framework.config.DwlConfig;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.bill.domain.Sellform;
import com.tudouji.project.bill.domain.Sellitem;
import com.tudouji.project.bill.domain.Transferapply;
import com.tudouji.project.bill.mapper.TransferapplyMapper;
import com.tudouji.project.bill.service.ISellformService;
import com.tudouji.project.bill.service.ISellitemService;
import com.tudouji.project.bill.service.ITransferapplyService;
import com.tudouji.project.farepay.domain.FarepayDetails;
import com.tudouji.project.farepay.service.IFarepayDetailsService;
import com.tudouji.project.flow.utils.FlowProcessUtil;
import com.tudouji.project.harvest.service.IFreightorderService;
import com.tudouji.project.organization.domain.Organization;
import com.tudouji.project.organization.service.IOrganizationService;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.impl.SalespermissionServiceImpl;
import com.tudouji.project.system.domain.SysUser;
import com.tudouji.project.system.domain.Tuser;
import com.tudouji.project.system.mapper.TuserMapper;
import com.tudouji.project.system.service.IBusinesscodeService;
import com.tudouji.project.system.service.ISysConfigService;
import com.tudouji.project.wlinfo.domain.Wlinfo;
import com.tudouji.project.wlinfo.domain.Wlinfoitem;
import com.tudouji.project.wlinfo.mapper.WlinfoMapper;
import com.tudouji.project.wlinfo.service.IWlinfoService;
import com.tudouji.project.wlinfo.service.IWlinfoitemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 物流信息Service业务层处理
 *
 * @author tudouji
 * @date 2021-10-27
 */
@Slf4j
@Service
public class WlinfoServiceImpl implements IWlinfoService {
    @Autowired
    private WlinfoMapper wlinfoMapper;
    @Autowired
    private TuserMapper tuserMapper;
    @Autowired
    private SalespermissionServiceImpl permissionService;

    @Autowired
    private IFarepayDetailsService fareDetailsService;

    @Autowired
    private IWlinfoService wlinfoService;

    @Autowired
    private IWlinfoitemService wlinfoItemService;

    @Autowired
    private ITransferapplyService transferService;
    @Autowired
    private TransferapplyMapper transferapplyMapper;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISellformService sellformService;

    @Autowired
    private ISellitemService sellitemService;

    @Autowired
    private IOrganizationService organizationService;
    @Autowired
    private FlowProcessUtil processUtils;
    @Autowired
    private FlowProcessProxy flowProcessProxy;
    @Autowired
    private IBusinesscodeService codeService;
    @Autowired
    private IFreightorderService freightorderService;

    /**
     * 查询物流信息
     *
     * @param guid 物流信息ID
     * @return 物流信息
     */
    @Override
    public Wlinfo selectWlinfoById(String guid) {
        return wlinfoMapper.selectWlinfoById(guid);
    }

    /**
     * 查询物流信息列表
     *
     * @param wlinfo 物流信息
     * @return 物流信息
     */
    @Override
    public List<Wlinfo> selectWlinfoList(Wlinfo wlinfo) {
        return wlinfoMapper.selectWlinfoList(wlinfo);
    }

    @Override
    public List<Wlinfo> selectWlinfoList(Wlinfo wlinfo, String functiontype) {
        return null;
    }

    @Override
    public List<Wlinfo> selectWlinfoList(Wlinfo info, String functiontype, Tuser user) {
        List<Wlinfo> list = new ArrayList<>();
        /*int flag = 0;
        List<Salespermission> permissions = permissionService.getPermByScAndFunc(user.getGuid(), functiontype, "4");
        if (permissions.size() > 0) {
            flag = 1;
        } else {
            permissions = permissionService.getPermByScAndFunc(user.getGuid(), functiontype, "2");
            if (permissions.size() > 0) {
                String[] departids = new String[permissions.size()];
                for (int i = 0; i < permissions.size(); i++) {
                    departids[i] = permissions.get(i).getDepartid();
                }
                info.setDepartids(departids);
                flag = 1;
            }
        }
        if (flag == 1) {*/
            list = wlinfoMapper.selectWlinfoListPemis(info);
//        }
        return list;
    }

    /**
     * 新增物流信息
     *
     * @param wlinfo 物流信息
     * @return 结果
     */
    @Override
    public int insertWlinfo(Wlinfo wlinfo) {
        return wlinfoMapper.insertWlinfo(wlinfo);
    }

    /**
     * 修改物流信息
     *
     * @param wlinfo 物流信息
     * @return 结果
     */
    @Override
    public int updateWlinfo(Wlinfo wlinfo) {

        return wlinfoMapper.updateWlinfo(wlinfo);
    }

    /**
     * 批量删除物流信息
     *
     * @param guids 需要删除的物流信息ID
     * @return 结果
     */
    @Override
    public int deleteWlinfoByIds(String[] guids) {
        return wlinfoMapper.deleteWlinfoByIds(guids);
    }

    /**
     * 删除物流信息信息
     *
     * @param guid 物流信息ID
     * @return 结果
     */
    @Override
    public int deleteWlinfoById(String guid) {
        return wlinfoMapper.deleteWlinfoById(guid);
    }

    @Override
    public List<Wlinfo> getWlInfoList(String orgid, String customerid, String startdate, String enddate,
                                      String functiontype, String status, String paystatus) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            SysUser sysUser = SecurityUtils.getLoginUser().getUser();
            Tuser tuser = tuserMapper.selectTuserById(sysUser.getTuserId());
            Wlinfo info = new Wlinfo();
            info.setOrgid(orgid);
            info.setCustomerid(customerid);
            if (StringUtils.isNotBlank(startdate)) {
                info.setStartdate(format.parse(startdate));
            }
            if (StringUtils.isNotBlank(enddate)) {
                info.setEnddate(format.parse(enddate));
            }
            if (StringUtils.isNotBlank(status)) {
                info.setStatus(Integer.parseInt(status));
            }
            if (StringUtils.isNotBlank(paystatus)) {
                info.setPaystatus(Integer.parseInt(paystatus));
            }
            info.setFaretype(1);//公司结算
            List<Wlinfo> wlinfos = this.selectWlinfoList(info, functiontype, tuser);
            return wlinfos;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @Override
    @Transactional
    public int deliver(String guid) {
        Wlinfo info = new Wlinfo();
        info.setGuid(guid);
        info.setStatus(WlinfoStatusEnums.DELIVERED.getCode());
        return this.updateWlinfo(info);
    }

    @Override
    public AjaxResult settle(Wlinfo wlinfo) {
        // 检查状态不为0 的 运费申请单的明细中 中是否已写入该物流单
        List<FarepayDetails> details = fareDetailsService.selectFarepayList(wlinfo.getGuid());
        if (details != null && details.size() > 0) {
            return AjaxResult.error("该物流单已发起付款申请，不允许再次结算");
        } else {
            if (wlinfo.getFaretype() == 1) { // 公司结算的 才进行金额计算
                switch (wlinfo.getFarepricetype()) {
                    case 1:// 按车
                        wlinfo.setBefdeductamount(wlinfo.getFareprice());
                        break;
                    case 2:// 按吨
                        wlinfo.setBefdeductamount(
                                wlinfo.getFareprice().multiply(wlinfo.getFareweight()).setScale(2, BigDecimal.ROUND_DOWN));
                        break;
                    default:
                        break;
                }
                wlinfo.setAmount(wlinfo.getBefdeductamount()
                        .add(wlinfo.getOthercost() == null ? BigDecimal.ZERO : wlinfo.getOthercost()));
            }
            wlinfo.setStatus(WlinfoStatusEnums.SETTLE.getCode());
            wlinfo.setPaystatus(WlinfoPayStatusEnum.DPAYING.getCode());// 结算确认后
            wlinfo.setCreatetime(null);
            this.updateWlinfo(wlinfo);
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult backSettle(String guid) {
        // 校验状态
        Wlinfo info = wlinfoMapper.selectWlinfoById(guid);
        if (info.getStatus() == WlinfoStatusEnums.SETTLE.getCode()
                && info.getPaystatus() == WlinfoPayStatusEnum.DPAYING.getCode()) {
            // 检查该单据 是否已写入 运费申请明细中
            FarepayDetails detail = new FarepayDetails();
            detail.setWlinfoGuid(guid);
            List<FarepayDetails> details = fareDetailsService.selectFarepayDetailsList(detail);
            if (details != null && details.size() > 0) {
                return AjaxResult.error("当前物流单不允许反结算");
            } else {
                info.setStatus(WlinfoStatusEnums.DELIVERED.getCode());
                this.updateWlinfo(info);
            }
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult checkOrder(String guid) {
        Map<String, String> map = new HashMap<String, String>();
        // 获取明细
        Wlinfoitem item = new Wlinfoitem();
        item.setWlinfoid(guid);
        List<Wlinfoitem> items = wlinfoItemService.selectWlinfoitemList(item);
//		if (items.size() > 1) {
//			for (Wlinfoitem i : items) {
//				if (i.getWlstatus().equals("1")) {
//					item = i;
//					break;
//				}
//			}
//		} else {
//
//		}
        item = items.get(0);
        if (item.getLinkcode().indexOf("SF") != -1) {// 销售 or 采收
            map.put("type", "SF");
        } else if (item.getLinkcode().indexOf("FO") != -1) {
            map.put("type", "FO");
        } else {// 调拨
            Transferapply apply = transferService.selectTransferapplyById(item.getOrderid());
            map.put("type", "TF");
            map.put("fromOrgId", apply.getOrgid());
            map.put("fromOrg", apply.getOrgname());
            map.put("toOrgId", apply.getToorgid());
            map.put("toOrg", apply.getToorgname());
        }
        return AjaxResult.success(map);
    }

    @Override
    public List<Wlinfo> exportWlInfoList(String orgid, String customerid, String startdate, String enddate
            , String status, String transtype) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Wlinfo info = new Wlinfo();
            info.setOrgid(orgid);
            info.setCustomerid(customerid);
            if (StringUtils.isNotBlank(startdate)) {
                info.setStartdate(format.parse(startdate));
            }
            if (StringUtils.isNotBlank(enddate)) {
                info.setEnddate(format.parse(enddate));
            }
            if (StringUtils.isNotBlank(status)) {
                info.setStatus(Integer.parseInt(status));
            }

            if (StringUtils.isNotBlank(status)) {
                info.setStatus(Integer.parseInt(status));
            } else {
                info.setMinStats(WlinfoPayStatusEnum.START.getCode());
            }
            if (StringUtils.isNotBlank(transtype)) {
                info.setTranstype(Integer.parseInt(transtype));
            }
            List<Wlinfo> wlinfos = this.findSOA(info);
            return wlinfos;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @Override
    public List<Wlinfo> findSOA(Wlinfo info) {
        return wlinfoMapper.findSOA(info);
    }

    @Override
    public AjaxResult checkWlInfo(String carCode, String orderid) {
        Wlinfo info = new Wlinfo();
        info.setCarcode(carCode);
        info.setOrderid(orderid);
        info = wlinfoMapper.getWlinfoByCarCode(info);
        return AjaxResult.success(info);
    }

    @Override
    public AjaxResult getWlDriver(String mobilephone) {
        Map<String, String> postMap = new HashMap<>();
        postMap.put("mobilephone", mobilephone);
        Map<String, String> company = HttpBusinessUtils.harvestPost(postMap,
                DwlConfig.getWebPath() + "/account/User!getDriver.action");

        JSONObject jsonObject = JSONObject.parseObject(company.get("result"));
        if (org.apache.commons.lang3.StringUtils.equals(jsonObject.getString("status"), "success")) {
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            if (jsonArray.size() > 0) {
                JSONObject driveInfo = jsonArray.getJSONObject(0);
                Map<String, Object> orderMap = null;
                try {
                    orderMap = this.getWlInfoByCarCode(driveInfo.getString("code"), 4);
                } catch (Exception e) {
                    e.printStackTrace();
                    return AjaxResult.error();
                }
                orderMap.put("driveInfo", driveInfo);
                return AjaxResult.success(JSONObject.parseObject(JSON.toJSONString(orderMap)));
            }

        }
        return AjaxResult.error();
    }

    /**
     * 获取物流信息
     *
     * @param @return
     * @return Wlinfo
     * @throws
     * @Title: getWlInfo
     * @Description:
     */
    public Map<String, Object> getWlInfoByCarCode(String code, int status) throws Exception {
        List<Sellform> sellforms = new ArrayList<>();
        List<Transferapply> transferapplys = new ArrayList<Transferapply>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("isOrder", "false");
        Wlinfo whinfo = new Wlinfo();
        whinfo.setCarcode(code);
        whinfo.setStatus(status);
        List<Wlinfo> whinfos = wlinfoMapper.findWlinfo(whinfo);
        for (Wlinfo info : whinfos) {
            for (Wlinfoitem item : info.getItems()) {
                //采收
                if (info.getType() == 1) {
                    resultMap.put("isOrder", "true");
                }
                //查询销售订单
                if (info.getType() == 2) {
                    Sellform sellform = new Sellform();
                    sellform.setGuid(item.getOrderid());
                    sellforms.add(sellformService.findDetail(sellform));
                    resultMap.put("isOrder", "true");
                }
                //查询调拨单
                if (info.getType() == 3) {
                    Transferapply transferapply = new Transferapply();
                    transferapply.setGuid(item.getOrderid());
                    transferapply = transferapplyMapper.findDetail(transferapply);
                    transferapplys.add(transferapply);
                    resultMap.put("isOrder", "true");

                }
            }
        }
        resultMap.put("sellforms", sellforms);
        resultMap.put("transferapplys", transferapplys);
        return resultMap;


    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public AjaxResult sendCar(Wlinfo info, Long orderId, String fileIds, Long taskid, String opinion) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Sellform sale = sellformService.selectSellformById(orderId);

        /** 2020/4/8新增 */
        Sellitem item = new Sellitem();
        item.setSellguid(sale.getGuid());
        List<Sellitem> items = sellitemService.selectSellitemList(item);
        String carcode = info.getCarcode();
        log.info("carcode: " + carcode);

        //写入物流信息
        info.setDeliprovince(sale.getDeliProvince());
        info.setDelicity(sale.getDeliCity());
        info.setDelidistrict(sale.getDistrict());
        info.setDeliaddress(sale.getDeliAddress());
        info.setDelicontact(sale.getDeliContact());
        info.setDelicontacttel(sale.getDeliContactNo());
        info.setArriprovince(sale.getArriProvince());
        info.setArricity(sale.getArriCity());
        info.setArridistrict(sale.getArriDistrict());
        info.setArriaddres(sale.getArriAddress());
        info.setArricontact(sale.getArriContact());
        info.setArricontactno(sale.getArriContactNo());
        info.setOrgid(sale.getSorgguid());

        Organization org = organizationService.selectOrganizationById(sale.getSorgguid());
        info.setOrgname(org.getName());
        sale.setCode(sale.getSellcode());
        sale.setPlatformId(sale.getStrmId());
        if (StringUtils.isNotBlank(info.getWlinfoId())) {
            info.setGuid(info.getWlinfoId());//拼车主订单id
            this.createWlGather(info.getWlinfoId(), info, sale, user);
        } else {
            this.createWlGather(info.getWlinfoId(), info, sale, user);
        }


        FlowResult result = flowProcessProxy.approval(taskid, fileIds, opinion, sale, user);
        if (result.getMsg().equals(ResultParam.SUCCESS)) {
            Sellform sl = new Sellform();
            sl.setStatus(result.getNextStat());
            sl.setId(orderId);
            sl.setCarcode(carcode);
            sellformService.updateSellform(sl);
            return AjaxResult.success();
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error(result.getMsg());
        }
    }

    @Override
    public AjaxResult toSendCar(Long orderid, Integer orderType) {
        AjaxResult result = AjaxResult.success();
        String orderguid = null;

        if (1 == orderType) {
//            Freightorder order = freightorderService.selectFreightorderById(orderid);
//            result.put("order", order);
//            orderguid = order.getGuid();
        } else if (2 == orderType) {
            Sellform order = sellformService.selectSellformById(orderid);
            result.put("order", order);
            orderguid = order.getGuid();
        }/* else if (3 == orderType) {
            Transferapply order = transferService.selectTransferapplyById(orderid);
            result.put("order", order);
        }*/

        Wlinfoitem wlinfoitem = new Wlinfoitem();
        wlinfoitem.setOrderid(orderguid);
        List<Wlinfoitem> wlinfoitems = wlinfoItemService.selectWlinfoitemList(wlinfoitem);
        List<Wlinfo> wlinfoList = new ArrayList<Wlinfo>();
        for (Wlinfoitem item : wlinfoitems) {
            Wlinfo wlinfo = this.wlinfoService.selectWlinfoById(item.getWlinfoid());
            wlinfoList.add(wlinfo);
        }

        result.put("wlinfos", wlinfoList);
        result.put("wlinfoitems", wlinfoitems);
        result.put("msg", "success");
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public AjaxResult deleteCar(String linkcode) {
        Wlinfoitem wlinfoitem = new Wlinfoitem();
        wlinfoitem.setLinkcode(linkcode);
        List<Wlinfoitem> wlinfoitemList1 = wlinfoItemService.selectWlinfoitemList(wlinfoitem);
        Wlinfo wlinfo = new Wlinfo();
        for (Wlinfoitem item : wlinfoitemList1) {
            Wlinfo newwlinfo = wlinfoMapper.selectWlinfoById(item.getWlinfoid());
            if (newwlinfo.getTranstype() == 1) {
                wlinfoitem = item;
                wlinfo = newwlinfo;
            }
        }

        if (wlinfoitem != null) {
            if (wlinfoitem.getWlstatus().equals(WlinfoitemStatusEnum.ZHU.getCode())) {
                return AjaxResult.error("不允许删除，请先删除拼车");
            } else if (wlinfoitem.getWlstatus().equals(WlinfoitemStatusEnum.CONG.getCode())) {
                //从信息，找以前的物流信息更新
                String wlguid = wlinfoitem.getWlinfoid();//一起的wlinfoid

                wlinfoitem.setWlstatus(WlinfoitemStatusEnum.DAN.getCode());
                Wlinfo infoByorder = this.wlinfoMapper.selectWlinfoByRelateid(wlinfoitem.getOrderid());
                infoByorder.setStatus(1);
                this.wlinfoMapper.updateWlinfo(infoByorder); //更新原物流信息为未派车
                wlinfoitem.setWlinfoid(infoByorder.getGuid());
                this.wlinfoItemService.updateWlinfoitem(wlinfoitem);//更新关联关系

                //查询主信息是否可以变更
                Wlinfoitem zhuWlinfoitem = new Wlinfoitem();
                zhuWlinfoitem.setWlinfoid(wlguid);
                List<Wlinfoitem> wlinfoitemList = this.wlinfoItemService.selectWlinfoitemList(zhuWlinfoitem);
                if (wlinfoitemList.size() == 1) {
                    Wlinfoitem zhuitem = wlinfoitemList.get(0);
                    zhuitem.setWlstatus(WlinfoitemStatusEnum.DAN.getCode());
                    this.wlinfoItemService.updateWlinfoitem(zhuitem);
                } else {
                }

            } else {
                //单
                if (wlinfo != null) {
                    wlinfo.setCarcode("");
                    wlinfo.setDrivername("");
                    wlinfo.setDriverphone("");
                    wlinfo.setCustomerid("");
                    wlinfo.setCustomername("");
                    wlinfo.setFareprice(new BigDecimal(0));
                    wlinfo.setStatus(1);
                    this.wlinfoMapper.updateWlinfo(wlinfo);
                    return AjaxResult.success("删除成功");
                } else {
                    return AjaxResult.error("删除失败");
                }
            }
        } else {
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }

    /**
     * @return
     * @Author shaodong
     * @Date 14:03 2021/9/26
     * @Param pinghce 1-否 2-是
     * @Param wlinfo 物流信息
     * @Param entity 业务信息
     * @Param user 当前用户
     * @Param
     **/
    public void createWlGather(String wlinfoguid, Wlinfo wlinfo, Sellform entity, SysUser user) {
        delWlInfo(entity.getGuid());
        if (StringUtils.isEmpty(wlinfoguid)) {
            String mainGuid = createWl(wlinfo, entity, user);
            createWlitem(mainGuid, entity.getGuid(), entity.getCode());
        } else {
            //如果是拼车--生成对应item
            createWlitem(wlinfoguid, entity.getGuid(), entity.getCode());
        }
    }

    public void delWlInfo(String orderId) {
        //校验是否有拼车订单
        Integer count = wlinfoItemService.findPinCount(orderId);
        Wlinfoitem item = wlinfoItemService.selectWlinfoitemByOrderid(orderId);
        if (count == 1) {//没有拼车信息，需要删除wlinfo

            Wlinfo info = wlinfoMapper.selectWlinfoById(item.getWlinfoid());
            if (info.getPaystatus() != WlinfoPayStatusEnum.START.getCode()) {
                throw new RuntimeException("物流派车");
            }
            wlinfoMapper.deleteWlinfoById(item.getWlinfoid());
        }
        if (item != null) {
            //删除item
            wlinfoItemService.deleteWlinfoitemById(item.getGuid());
        }
    }

    /**
     * @return
     * @Author shaodong
     * @Description 保存物流主表信息
     * @Date 10:52 2021/9/26
     * @Param
     **/
    public String createWl(Wlinfo wlinfo, Sellform entity, SysUser user) {
        wlinfo.setOperatorid(user.getGuid());
        wlinfo.setOperator(user.getNickName());
        wlinfo.setCode(codeService.getCode("2"));
        wlinfo.setStatus(WlinfoStatusEnum.UNDELIVER.getCode());
        wlinfo.setFlowid("16");
        wlinfo.setRelatedid(entity.getGuid());
        wlinfo.setDepartid(entity.getDepguid());
        wlinfo.setDepartname(entity.getDepname());
        wlinfo = setWlSort(wlinfo, entity.getCode());
        //销售订单下单组织写入物流单的结算组织 ,销售sorgguid 调拨的toorgid
        wlinfo.setPaystatus(WlinfoPayStatusEnum.START.getCode());

        System.out.println("-------whlinfo org --------" + wlinfo.getOrgid());
        System.out.println("-------whlinfo org --------" + wlinfo.getOrgname());

        if (StringUtils.isBlank(wlinfo.getOrgid())) {
            wlinfo.setOrgid(entity.getSorgguid());
        }
        if (StringUtils.isBlank(wlinfo.getOrgname())) {
            wlinfo.setOrgname(entity.getOrgName());
        }
        String guid = IdUtils.fastSimpleUUID();
        wlinfo.setGuid(guid);
        wlinfoMapper.insertWlinfo(wlinfo);
        return guid;
    }

    /**
     * @return
     * @Author shaodong
     * @Description 保存物流明细信息
     * @Date 10:52 2021/9/26
     * @Param
     **/
    private void createWlitem(String mainGuid, String orderid, String code) {
        Wlinfoitem item = new Wlinfoitem();
        item.setWlinfoid(mainGuid);
        item.setLinkcode(code);
        item.setOrderid(orderid);
        item.setWlstatus("0");
        item.setGuid(IdUtils.fastSimpleUUID());
        wlinfoItemService.insertWlinfoitem(item);
    }

    /**
     * @return
     * @Author shaodong
     * @Description 物流订单排序设置不论主订单还是普通订单，一律生成sort字段=
     * 年月日时分+主销售订单编号+100（99，98。。。）
     * @Date 10:22 2020/10/13
     * @Param
     **/
    public Wlinfo setWlSort(Wlinfo wlinfo, String sellcode) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmm");
        Date now = new Date();
        wlinfo.setCreatetime(now);
        wlinfo.setSort(simpleDateFormat.format(now) + sellcode + "99");
        return wlinfo;
    }

}
